namespace Be
{
	template<typename T>
	System<T>::System()
	{
	}

	template<typename T>
	System<T>::~System()
	{
	}

	template<typename T>
	bool System<T>::Register(Entity* p_pEntity, typename const T::Seed& p_rSeed)
	{
		assert(p_pEntity != 0);
		assert(m_NodeMap.find(p_pEntity) == m_NodeMap.end());

		T* l_pNode = new T(p_pEntity);

		p_pEntity->AddNode(l_pNode);
		m_NodeMap[p_pEntity] = l_pNode;
		this->OnRegister(l_pNode, p_rSeed);

		return true;
	}

	template<typename T>
	void System<T>::Unregister(Entity* p_pEntity)
	{
		assert(p_pEntity != 0);

		std::map<Entity*, typename T*>::iterator l_Iter = m_NodeMap.find(p_pEntity);
		assert(l_Iter != m_NodeMap.end());

		T* l_pNode = l_Iter->second;

		this->OnUnregister(l_pNode);
		m_NodeMap.erase(l_Iter);
		p_pEntity->RemoveNode(l_pNode);

		delete l_pNode;
	}

	template<typename T>
	void System<T>::Update()
	{
		std::map<Entity*, typename T*>::iterator l_Iter = m_NodeMap.begin();
		while(l_Iter != m_NodeMap.end())
		{
			std::map<Entity*, typename T*>::iterator l_CurIter = l_Iter;
			++l_Iter;

			T* l_pNode = l_CurIter->second;
			assert(l_pNode != 0);

			if(l_pNode->m_IsValid == false)
			{
				this->OnUnregister(l_pNode);
				m_NodeMap.erase(l_CurIter);

				delete l_pNode;

				continue;
			}

			OnUpdate(l_pNode);
		}
	}

	template<typename T>
	T* System<T>::GetNode(Entity* p_pEntity)
	{
		std::map<Entity*, typename T*>::iterator l_Iter = m_NodeMap.find(p_pEntity);
		assert(l_Iter != m_NodeMap.end());

		return l_Iter->second;
	}
}